package com.csi.bumblebee.util;

import com.csi.bumblebee.domain.dictionary.JobExam;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelReader {

    public ExcelReader() {
        throw new Error("工具类不允许实例化！");
    }

    /**
     * 获取并解析excel文件，返回一个二维集合
     * @param file 上传的文件
     * @return 二维集合（第一重集合为行，第二重集合为列，每一行包含该行的列集合，列集合包含该行的全部单元格的值）
     */
    public static List<JobExam> analysis(MultipartFile file) {

        List<JobExam> jobExamList = new ArrayList<>();
        //获取文件名称
        String fileName = file.getOriginalFilename();
        System.out.println(fileName);

        try {
            //获取输入流
            InputStream in = file.getInputStream();
            //判断excel版本
            Workbook workbook = new XSSFWorkbook(in);

            //获取第一张工作表
            Sheet sheet = workbook.getSheetAt(0);
            //从第二行开始获取
            for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
                //循环获取工作表的每一行
                Row sheetRow = sheet.getRow(i);

                JobExam jobExam = new JobExam();

                //设置内容
                String content = sheetRow.getCell(0).getStringCellValue();
                jobExam.setContent(content);

                //设置答案
                String answer = sheetRow.getCell(1).getStringCellValue();
                jobExam.setAnswer(answer);

                //设置分值
                sheetRow.getCell(2).setCellType(CellType.STRING);
                String scoreString = sheetRow.getCell(2).getStringCellValue();
                int score = 0;
                if (JudgeDataType.isInteger(scoreString, true)) {
                    score = Integer.parseInt(scoreString);
                }
                jobExam.setScore(score);

                //设置选项A
                jobExam.setOptionA(sheetRow.getCell(3).getStringCellValue());

                //设置选项B
                jobExam.setOptionB(sheetRow.getCell(4).getStringCellValue());

                //设置选项C
                jobExam.setOptionC(sheetRow.getCell(5).getStringCellValue());

                //设置选项D
                jobExam.setOptionD(sheetRow.getCell(6).getStringCellValue());

                //设置技能类型Id
                jobExam.setSkillTypeId(sheetRow.getCell(7).getStringCellValue());

                //设置题目类型Id
                jobExam.setExamTypeId(sheetRow.getCell(8).getStringCellValue());

                //设置岗位类型Id
                jobExam.setJobTypeId(sheetRow.getCell(9).getStringCellValue());

                //将装有每一列的集合装入大集合
                jobExamList.add(jobExam);

                //关闭资源
                workbook.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("===================未找到文件======================");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("===================上传失败======================");
        }

        return jobExamList;
    }

}